<?php
require_once 'BaseController.php';
class Admin_AdsController extends BaseController
{
    public function init()
    {
        Zend_Loader::loadClass('AuthModel');
        Zend_Loader::loadClass('AdminModel');
        Zend_Loader::loadClass('AdvertiseModel');
    }
    public function indexAction()
    {
        if (isset($_GET['ajax'])) {
            $options = array('json' => 1);
            $model = new AdvertiseModel();
            echo $model->listAdsForDB('', $options);
            exit();
        }
    }
    public function addAction()
    {
        if ($this->_request->isPost()) {
            $model = new AdvertiseModel();
            
            $page = $this->_request->getParam('select-page');
            $position = $this->_request->getParam('select-pos');
            
            //Duyet qua 1 luot de lay sort tat ca quang cao cung vi tri va page
            $allAds = $model->listAdsByPos($page,$position);
            if($allAds){
                $max_sort = intval($allAds[0]['sort']);
            }else $max_sort = 0;
            //End duyet
            
            $sort = $this->_request->getParam('select-sort');
            if ($sort == 'first')
                $sort_num = 1;
            else
                if ($sort == 'end')
                    $sort_num = $max_sort+1;
                else
                    $sort_num = intval($this->_request->getParam('sort'));
            //Cap nhat lai thu tu tat ca cac quang cao
            if($sort != 'end' && $sort_num < $max_sort+1){
                foreach($allAds as $ads){
                    if(intval($ads['sort']) >= $sort_num){
                        $ads['sort'] = intval($ads['sort']) + 1;
                        $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                    }
                }
            }else $sort_num = $max_sort+1;
            //End Cap nhat
            
            $pictures = stripcslashes($this->_request->getParam('urlSelectImg'));
            $description = $this->_request->getParam('description');
            $link = stripcslashes($this->_request->getParam('link'));
            $status = $this->_request->getParam('status');
            $arrParam = array(
                'page' => $page,
                'position' => $position,
                'sort' => $sort_num,
                'pictures' => $pictures,
                'link' => $link,
                'description' => $description,
                'status' => $status
            );
            if($pictures != null && $pictures != ''){
                $model->insert($arrParam);  
                $this->_redirect('/admin/ads/index'); 
            }else{
                $this->view->arrParam = $arrParam;
                $this->view->warning = 'Chưa up banner quảng cáo';
            }
        }
    }
    public function editAction(){
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $model = new AdvertiseModel();
            $data = $model->adsById($id);
            $this->view->data = $data;
        }
    }
    public function doneeditAction()
    {
        if ($this->_request->isPost()) {
            $model = new AdvertiseModel();
            //Lay cac gia tri cu
            $old_page = $this->_request->getParam('old_page');
            $old_pos = $this->_request->getParam('old_pos');
            $old_sort = $this->_request->getParam('old_sort');
            //End
            $id = $this->_request->getParam('id');
            
            $page = $this->_request->getParam('select-page');
            $position = $this->_request->getParam('select-pos');
            
            //Duyet qua 1 luot de lay sort tat ca quang cao cung vi tri va page
            $allAds = $model->listAdsByPos($page,$position);
            if($allAds){
                    $max_sort = intval($allAds[0]['sort']);
                }else $max_sort = 0;
            //End duyet
            $sort = $this->_request->getParam('select-sort');
                if ($sort == 'first')
                    $sort_num = 1;
                else
                    if ($sort == 'option')
                        $sort_num = intval($this->_request->getParam('sort'));    
            
            //Neu page va position moi thi xu ly nhu them quang cao moi
            if($old_page != $page || $old_pos != $position){
                //Cap nhat lai thu tu o muc cu
                   $allOldAds = $model->listAdsByPos($old_page,$old_pos);
                   if($allOldAds){
                    $max_old_sort = intval($allOldAds[0]['sort']);
                        if($old_sort < $max_old_sort){
                            foreach($allOldAds as $ads){
                                if(intval($ads['sort']) > $old_sort){
                                    $ads['sort'] = intval($ads['sort']) - 1;
                                    $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                                }
                            }
                        } 
                    }
                //END cu
                //Cap nhat lai thu tu tat ca cac quang cao o muc moi
                if($sort != 'end' && $sort_num < $max_sort+1){
                    foreach($allAds as $ads){
                        if(intval($ads['sort']) >= $sort_num){
                            $ads['sort'] = intval($ads['sort']) + 1;
                            $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                        }
                    }
                }else $sort_num = $max_sort+1;
                //End Cap nhat
            }else{//Neu page va position van giu nguyen chi thay doi sort
                if($sort == 'end') $sort_num = $max_sort;
                if($sort_num > $max_sort) $sort_num = $max_sort;
                if($sort_num > $old_sort){
                    foreach($allAds as $ads){
                        if(intval($ads['sort']) > $old_sort){
                            $ads['sort'] = intval($ads['sort']) - 1;
                            $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                        }
                    }
                }else if($sort_num < $old_sort){
                    foreach($allAds as $ads){
                        if(intval($ads['sort']) < $old_sort){
                            $ads['sort'] = intval($ads['sort']) + 1;
                            $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                        }
                    }
                }
            }
            
            $pictures = stripcslashes($this->_request->getParam('urlSelectImg'));
            $description = $this->_request->getParam('description');
            $link = stripcslashes($this->_request->getParam('link'));
            $status = $this->_request->getParam('status');
            $arrParam = array(
                'page' => $page,
                'position' => $position,
                'sort' => $sort_num,
                'pictures' => $pictures,
                'link' => $link,
                'description' => $description,
                'status' => $status
            );
            if($pictures != null && $pictures != ''){
                $model->update($arrParam,array('id='.$id));  
                $this->_redirect('/admin/ads/index'); 
            }else{
                $this->view->arrParam = $arrParam;
                $this->view->warning = 'Chưa up banner quảng cáo';
            }
        }
    }
    public function deleteAction()
    {
        $rows = $this->_request->getParam('checks');
        $model = new AdvertiseModel();
        for($i = 0;$i<count($rows);$i++){
            $ads = $model->adsById($rows[$i]);
            $page = $ads[0]['page'];
            $pos = $ads[0]['position'];
            $sort_num = $ads[0]['sort'];
            $allAds = $model->listAdsByPos($page,$pos);
            foreach($allAds as $ads){
                    if(intval($ads['sort']) > $sort_num){
                        $ads['sort'] = intval($ads['sort']) - 1;
                        $model->update(array('sort' => $ads['sort']),array('id ='.$ads['id']));
                    }
            }
        }
        
        $model->multiDelete($rows);
        $this->_redirect('/admin/ads/index');
        exit();
    }
}
